package com.alibaba.cirtus.ots.application.adapter.ability.facade.service.sap;

import com.alibaba.acm.shaded.com.alibaba.metrics.StringUtils;
import com.alibaba.acm.shaded.com.google.common.collect.Lists;
import com.alibaba.cirtus.ots.application.adapter.ability.utils.RequestUtils;
import com.alibaba.citrus.ots.common.annotation.FacadeInvoker;
import com.alibaba.citrus.ots.common.annotation.ProcessInvoker;
import com.alibaba.cz.base.tool.log.Log;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.epoch.app.bcots.model.dto.BaselineBatchQueryChannelsRequest;
import com.epoch.app.bcots.model.dto.BaselineGetOrganizationByIdRequest;
import com.epoch.app.bcots.service.BaseDataService;
import com.epoch.app.bcots.service.PlatformEnhanceService;
import com.epoch.app.otsapplicationadaptersdk.sap.MnSapIf002QuerySDO;
import com.epoch.app.otsapplicationadaptersdk.sap.MnSapIf002QuerySDOList;
import com.epoch.app.otsapplicationadaptersdk.sap.MnSapIf002Request;
import com.epoch.app.otsapplicationadaptersdk.sap.MnSapIf002Response;
import com.epoch.app.otsapplicationadaptersdk.sap.mnsapif002.dto.DoHandlerRequest;
import com.epoch.app.otsapplicationadaptersdk.sap.mnsapif002.dto.DoHandlerResponse;
import com.epoch.app.otsapplicationadaptersdk.sap.mnsapif002.service.MnSapIf002Service;
import com.epoch.app.otsapplicationadaptersdk.service.SapService;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Component;

import org.springframework.beans.BeanUtils;

import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @author hanguo
 * @date 2021/10/12
 * @Description 信贷余额&账期检查实时查询接口
 */
@Component
@Primary
public class MnSapIf002ServiceImpl implements MnSapIf002Service {
    private static Log log = Log.getLogger(MnSapIf002ServiceImpl.class);

    @Autowired
    private BaseDataService baseDataService;
    @Autowired
    private SapService sapService;

    @ProcessInvoker(remark = "MnSapIf002Service")
    @Override
    public DoHandlerResponse doHandler(DoHandlerRequest doHandlerRequest) {
        MnSapIf002Request mnSapIf002Request = new MnSapIf002Request();
        mnSapIf002Request.setMessageHeader(RequestUtils.generateMessageHeader("MnSapIf002", "SAP"));

        List<MnSapIf002QuerySDO> list = Lists.newArrayList();

        MnSapIf002QuerySDO mnSapIf002QuerySDO = new MnSapIf002QuerySDO();
        mnSapIf002QuerySDO.setCustomerId(doHandlerRequest.getCustomerId());
        mnSapIf002QuerySDO.setScItemGroupId(doHandlerRequest.getScItemGroupId());
        mnSapIf002QuerySDO.setSaleOrderId(doHandlerRequest.getSaleOrderId());

        String organizationId = doHandlerRequest.getOrganizationId();
        String channelId = doHandlerRequest.getChannelId();
        if (StringUtils.isNotBlank(organizationId)) {
            //调用基线API获取sap的组织编码
            BaselineGetOrganizationByIdRequest baselineGetOrganizationByIdRequest = new BaselineGetOrganizationByIdRequest();
            baselineGetOrganizationByIdRequest.setId(organizationId);
//            Map map = doHandler$callBaseLineGetOrganizationById(baselineGetOrganizationByIdRequest);
            Map map = (Map) baseDataService.baselineGetOrganizationById(baselineGetOrganizationByIdRequest);
            if (Objects.nonNull(map)) {
                Map<String, Object> resultMap = (Map<String, Object>) map.get("result");
                if (Objects.nonNull(resultMap)) {
                    Map<String, Object> resultSubMap = (Map<String, Object>) resultMap.get("result");
                    if (Objects.nonNull(resultSubMap)) {
                        Map<String, Object> organizationSDO = (Map<String, Object>) resultSubMap.get("organizationSDO");
                        if (Objects.nonNull(organizationSDO)) {
                            mnSapIf002QuerySDO.setOrganizationId((String) organizationSDO.get("outCode"));
                        }
                    }
                }
            }
        }

        if (StringUtils.isNotBlank(channelId)) {
            BaselineBatchQueryChannelsRequest baselineBatchQueryChannelsRequest = new BaselineBatchQueryChannelsRequest();
            baselineBatchQueryChannelsRequest.setChannelCodes(Lists.newArrayList(channelId));
            //调用基线API获取sap渠道编码
//            Map map = doHandler$callBaselineBatchQueryChannels(baselineBatchQueryChannelsRequest);
            Map map = (Map) baseDataService.baselineBatchQueryChannels(baselineBatchQueryChannelsRequest);
            if (Objects.nonNull(map)) {
                Map<String, Object> resultMap = (Map<String, Object>) map.get("result");
                if (Objects.nonNull(resultMap)) {
                    List<Map<String, Object>> resultList = (List<Map<String, Object>>) resultMap.get("result");
                    if (CollectionUtils.isNotEmpty(resultList)) {
                        Map<String, Object> resultSubMap = resultList.get(0);
                        mnSapIf002QuerySDO.setChannelId((String) resultSubMap.get("outCode"));
                    }

                }
            }
        }
        list.add(mnSapIf002QuerySDO);
        mnSapIf002Request.setData(list);
        log.info("查询信贷mnSapIf002Request {}", JSONObject.toJSONString(mnSapIf002Request));
        MnSapIf002Response mnSapIf002Response = sapService.mnSapIf002CallREST(mnSapIf002Request);
        log.info("mnSapIf002Response结果:{}", JSON.toJSONString(mnSapIf002Response));
        DoHandlerResponse doHandlerResponse = new DoHandlerResponse();
        if (Objects.nonNull(mnSapIf002Response)) {
            doHandlerResponse.setMessageHeader(mnSapIf002Response.getMessageHeader());
            doHandlerResponse.setData(mnSapIf002Response.getData().get(0));
        }
        return doHandlerResponse;
    }
}
